<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="所属银行" prop="tableName">
        <el-select v-model="queryParams.tableName" placeholder="请选择所属银行" clearable >
          <el-option label="蓝海银行" value="ecs_pay_ease" />
          <el-option label="蓝海/苏商/厦门" value="ecs_yee_pay" />
          <el-option label="苏商/华通" value="ecs_baofu_merchant" />
        </el-select>
      </el-form-item>
      <el-form-item label="公司名称" prop="signedShorthand">
        <el-input
          v-model="queryParams.signedShorthand"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属平台" v-if="user.userId === 1" prop="platformNickName">
        <el-input
          v-model="queryParams.platformNickName"
          placeholder="请输入所属平台"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属代理" v-if="user.deptId < 101" prop="agentNickName">
        <el-input
          v-model="queryParams.agentNickName"
          placeholder="请输入所属代理"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属二级代理" v-if="user.deptId < 102" prop="childAgentNickName">
        <el-input
          v-model="queryParams.childAgentNickName"
          placeholder="请输入所属二级代理"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户昵称" v-if="user.deptId < 102" prop="shopNickName">
        <el-input
          v-model="queryParams.shopNickName"
          placeholder="请输入商户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="separateAccountsList">
      <el-table-column label="公司名称" fixed="left" align="center" prop="signedShorthand" width="300">
        <template slot-scope="{ row }">
          {{ row.signedShorthand }}
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center" prop="name">
        <template slot-scope="{ row }">
          {{ row.name }}
        </template>
      </el-table-column>
      <el-table-column label="所属平台" show-overflow-tooltip v-if="user.userId === 1" align="center" prop="platformNickName" width="150">
        <template slot-scope="{ row }">
          {{ row.platformNickName }}
        </template>
      </el-table-column>
      <el-table-column label="所属代理" show-overflow-tooltip v-if="user.deptId < 101" align="center" prop="agentNickName" width="150">
        <template slot-scope="{ row }">
          {{ row.agentNickName }}
        </template>
      </el-table-column>
      <el-table-column label="所属二级代理" v-if="user.deptId < 102" align="center" prop="childAgentNickName" width="150">
        <template slot-scope="{ row }">
          {{ row.childAgentNickName }}
        </template>
      </el-table-column>
      <el-table-column :label="user.userId === 1?'平台余额':'余额'"  v-if="user.userId === 1 || user.deptId === 100" align="center" prop="platformBalance" width="120">
        <template slot-scope="{ row }">
          {{ row.platformBalance }}
        </template>
      </el-table-column>
      <el-table-column :label="user.userId === 1?'代理余额':'余额'"  v-if="user.userId === 1 || user.deptId === 101" align="center" prop="agentBalance" width="120">
        <template slot-scope="{ row }">
          {{ row.agentBalance }}
        </template>
      </el-table-column>
      <el-table-column :label="user.userId === 1?'二级代理余额':'余额'"  v-if="user.userId === 1 || user.deptId === 102" align="center" prop="childAgentBalance" width="120" >
        <template slot-scope="{ row }">
          {{ row.childAgentBalance }}
        </template>
      </el-table-column>
      <el-table-column :label="user.userId === 1?'店铺余额':'余额'" v-if="user.userId === 1 || user.deptId === 200" align="center" prop="shopBalance" width="120">
        <template slot-scope="{ row }">
          {{ row.shopBalance }}
        </template>
      </el-table-column>
      <template  v-if="user.userId !== 1">

        <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handTransfer(scope.row)"
              v-hasPermi="['ecs:externalTransfer:externalTransfer']"
            >转账</el-button>
          </template>
        </el-table-column>
      </template>

    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :close-on-click-modal="false" @closed="createClose" :close-on-press-escape="false" :title="cashTitle" :visible.sync="cashOpen" width="500px" append-to-body>
      <el-form ref="form" :rules="rules" :model="externalTransfer" size="small" :inline="true" label-width="80px">
        <el-form-item label="手机号">
          <el-col :span="16">
            <el-input v-model="user.phonenumber" placeholder="请输入手机号" disabled />
          </el-col>
          <el-col :span="8">
            <el-link @click="href" :underline="false" type="primary" >立即配置</el-link>
          </el-col>
        </el-form-item>
        <el-form-item label="验证码">
          <el-col :span="16">
            <el-input v-model="smsCode" placeholder="请输入短信验证码" />
          </el-col>
          <el-col :span="8">
            <el-button type="primary" style="width: 100%;" plain :disabled="sendingSms" @click="sendSms">{{sendSmsText}}</el-button>
          </el-col>
        </el-form-item>
        <el-form-item label="收款账户" prop="externalTransferAccountId">
          <el-select filterable  v-model="externalTransfer.externalTransferAccountId" placeholder="请选择收款账户" clearable >
            <el-option v-for="item in externalTransferAccountList" :label="item.name" :value="item.id" :key="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="转账金额" prop="amount">
          <el-input-number :step="1" :min="0" :max="user.deptId === 100?form.platformBalance:user.deptId === 101?form.agentBalance: user.deptId === 102?form.childAgentBalance:user.deptId === 200?form.shopBalance:1" :precision="2" v-model.number="externalTransfer.amount" placeholder="请输入转账金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input :maxlength="64" v-model="externalTransfer.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="balaLoading" @click="submitCash" v-clickOnce>确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShopIncoming } from '@/api/ecs/shopIncoming'
import { externalTransfer } from '@/api/ecs/externalTransfer'
import { createCode, verifyCode } from '@/api/ecs/sms'
import { getUserProfile } from '@/api/system/user'
import { getAgentUserId } from '@/api/ecs/agent'
import { listExternalTransferAccount } from '@/api/ecs/externalTransferAccount'

export default {
  name: "SeparateAccounts",
  data() {
    return {
      // 遮罩层
      loading: true,
      balaLoading:false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      isAll:false,
      cashOpen:false,
      withdrawalAccountOpen:false,
      sendSmsText: "发送短信",
      cashTitle: "一键转账短信验证",
      separateAccountsList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: 9,
        signedShorthand:"",
        shopNickName:"",
        childAgentNickName:"",
        agentNickName:"",
        platformNickName:"",
      },
      productList:[],
      smsCode:'',
      sendingSms:false,
      user:{},
      form:{
        shopBalance:"",
        childAgentBalance:"",
        agentBalance:"",
        platformBalance:"",
      },
      externalTransfer:{
        amount:0,
        remark:"",
        shopIncomingId:"",
        externalTransferAccountId:""
      },
      externalTransferAccountList:[],
      rules: {
        amount: [
          { required: true, message: "转账金额不能为空", trigger: "blur" },
          { type: 'number', min: 1, message: "转账金额不能低于1元", trigger: ['blur',"change"] }
        ],
        remark: [
          { max:64, message: "转账备注最大64字符", trigger: "blur" }
        ],
        externalTransferAccountId: [
          { required: true, message: "请选择转账接接收方", trigger: "change" }
        ],
      },
      withdrawalAccountDetail:{},
      withdrawalAccountData:[],
    };
  },
  created() {

    getUserProfile().then(response => {
      this.user = response.data;
      if (this.user.deptId === 101){
        getAgentUserId(this.user.userId).then(response => {
          if(response.data.agentId !== 0){
            this.user.deptId = 102
          }
        });
      }
    });
    this.getList();
  },
  mounted() {
    // listExternalTransferAccount({pageSize:100000}).then(response => {
    //   this.externalTransferAccountList = response.rows;
    // });
  },
  methods: {
    /** 查询所有三方入金后分账记录列表 */
    getList() {
      this.loading = true;
      listShopIncoming(this.queryParams).then(response => {
        this.separateAccountsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    href(){
      this.$router.push('/user/profile')
    },
    submitCash(){
      // if (this.user.userId === 402){
      //   this.$modal.msgError("暂时无法提现，请联系客服");
      //   return;
      // }
      if(! this.smsCode){
        this.$modal.msgError("请填写短信验证码");
      }else{
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.balaLoading = true
            verifyCode({
              code: this.smsCode
            }).then(res => {
              if (res.data === true) {
                this.handleExternalTransfer()
              } else {
                this.$modal.msgError("无效的短信验证码");
              }
            })
          }
        })
      }
    },
    handTransfer(row){
      this.form = row;
      this.cashTitle='对 '+row.signedShorthand+' 转账';
      this.cashOpen=true
      this.balaLoading = true
      listExternalTransferAccount({pageSize:100000,tableName:row.tableName}).then(response => {
        this.balaLoading = false
        this.externalTransferAccountList = response.rows;
      }).catch(()=>{
        this.balaLoading = false
      })
    },
    sendSms(){
      if (!this.user.phonenumber){
        this.$modal.msgError("手机号不能为空");
        return
      }
      createCode({}).then(response => {
        if(response.code === 200){
          this.sendingSms = true;
          let countSec = 60
          let intervalId = setInterval(()=>{
            countSec--;
            this.sendSmsText = countSec+"秒后可重发";
            if(countSec <= 0){
              clearInterval(intervalId);
              this.sendingSms = false;
              this.sendSmsText = "发送`短信";

            }
          },1000)
        }
      })
    },
    handleExternalTransfer(){
      this.externalTransfer.shopIncomingId = this.form.id
      externalTransfer(this.externalTransfer).then(res=>{
        this.smsCode = ""
        this.balaLoading = false
        if (res.code === 200){
          if (res.data === true){
            this.cashOpen = false
            this.$modal.msgSuccess("转账中.......");
          }
        }
      }).catch(()=>{
        this.balaLoading = false
      })
    },
    createClose(){
      this.smsCode = "";
      this.resetForm("form");
    }
  }
};
</script>
